package com.daiqee.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.daiqee.pojo.Reimburse;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.daiqee.dao.mapper.ReimburseMapper;

/**
 * 报销
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月9日
 * @package com.daiqee.service.impl
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Service
public class ReimburseService extends ServiceImpl<ReimburseMapper, Reimburse>{

	@Value("${pic.receipt}")
	private String receiptImgSavePath;
	
	@Autowired
	private ReimburseMapper mapper;

	public Boolean saveReceiptImg(MultipartFile receiptImages, String id) throws IllegalStateException, IOException {
		
		String oldFileName = receiptImages.getOriginalFilename(); // 获取上传文件的原名
		
		if(oldFileName != null && oldFileName.length() > 0) {
			
			// 新的图片名称
	        String newFileName = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SS").format(new Date())
	        		+ "-" + oldFileName;
	        
	        // 新图片
	        File newFile = new File(receiptImgSavePath + "\\" + newFileName);
	        
	        // 将内存中的数据写入磁盘
	        receiptImages.transferTo(newFile);
	        
	        Reimburse reimburse = mapper.selectById(id);
	        
	        if(reimburse.getReimburseReceiptPath() == null) {
	        	reimburse.setReimburseReceiptPath("");
	        }
	        
	        reimburse.setReimburseReceiptPath(reimburse.getReimburseReceiptPath() + "|" + newFileName);
	        
	        mapper.updateById(reimburse);
	        
		}else {
			
			return false;
		}
		
		return true;
	}
	
}
